﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;


namespace x.paypal.model
{
    public class PayRequest:IPayRequest
    {
        /// <summary>
        /// We always send XML
        /// </summary>
        XDocument doc = null;
        XNamespace payPalNamespace = "http://svcs.paypal.com/types/ap";
        string request = "PayRequest"; 
        string PayReference; 
        #region IPayRequest Members

        public IPayRequest CreateRequest()
        {

            doc = new XDocument
            (

               new XElement(payPalNamespace + request,
                   new XElement("requestEnvelope",
                   new XElement("errorLanguage", "en_US"))
                   ));
            XElement a = new XElement("actionType",ActionType.PAY.ToString());
            doc.Element(payPalNamespace + "PayRequest").Add(a);           
            return this; 
        }
        public IPayRequest WithPaymentReference(string paymentReference)
        {
            this.PayReference = paymentReference;
            return this;  
        }
        public IPayRequest WithReturnUri(Uri returnUri)
        {
            XElement a = new XElement("returnUrl", returnUri);
            doc.Element(payPalNamespace + "PayRequest").Add(a);
            return this;
        }
        public IPayRequest WithCancelUri(Uri cancelUri)
        {
            XElement a = new XElement("cancelUrl", cancelUri);
            doc.Element(payPalNamespace + "PayRequest").Add(a);
            return this;
        }
        public IPayRequest WithIPNUri(Uri ipnUri)
        {
            XElement a = new XElement("ipnNotificationUrl", ipnUri);
            doc.Element(payPalNamespace + "PayRequest").Add(a);
            return this;
        }
        public IPayRequest ReceiverList(IList<Receiver> receiverList)
        {
            XElement rc =
                new XElement("receiverList",
                        from r in receiverList
                        select new XElement("receiver",
                         new XElement("email", r.Email),
                         new XElement("primary", r.IsPrimary),
                         new XElement("invoiceId", r.InvoiceId),
                         new XElement("description", r.Description),
                         //new XElement("phone",
                         //    new XElement("countrycode",r.Phone.CountryCode),
                         //    new XElement("extension", r.Phone.Extenstion),
                         //    new XElement("phoneNumber", r.Phone.Number)),
                         new XElement("amount",r.Amount)));
            doc.Element(payPalNamespace + "PayRequest").Add(rc);
            return this;
        }

        public IPayRequest Currency(string currency)
        {
            XElement c = new XElement("currencyCode", currency.Trim());
            doc.Element(payPalNamespace + "PayRequest").Add(c);
            return this; 
        }
        public IPayRequest WithTrackingId(string trackingId)
        {
            XElement t = new XElement("trackingId", trackingId);
            doc.Element(payPalNamespace + "PayRequest").Add(t);
            return this;
        }
        public IPayRequest WithClientDetails()
        {
            return this;
        }
        
        public XElement AsXML
        {
            
            get 
            {
                return XElement.Parse(doc.ToString()); 
            }
        }

        public string PaymentReference
        {

            get
            {
                return this.PayReference; 
            }
        }
        #endregion
    }
}